package net.sunofbeach.blog.services.impl;


import lombok.extern.slf4j.Slf4j;
import net.sunofbeach.blog.dao.PdfDao;
import net.sunofbeach.blog.pojo.Pdf;
import net.sunofbeach.blog.pojo.SobUser;
import net.sunofbeach.blog.response.ResponseResult;
import net.sunofbeach.blog.services.IPdfService;
import net.sunofbeach.blog.services.IUserService;
import net.sunofbeach.blog.utils.Constants;
import net.sunofbeach.blog.utils.IdWorker;
import net.sunofbeach.blog.utils.TextUtils;
import net.sunofbeach.blog.utils.UploadPdfToCosUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.transaction.Transactional;
import java.io.IOException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

@Slf4j
@Service
@Transactional
public class IPdfServiceImpl extends BaseService implements IPdfService {


    @Value("${sob.blog.image.max-size}")
    private Long maxSize;

    @Autowired
    private IdWorker idWorker;

    @Autowired
    private IUserService userService;

    @Autowired
    private PdfDao pdfDao;


    @Override
    public ResponseResult cosUploadPdf( MultipartFile file) {
        if (file == null) {
            return ResponseResult.FAILED("pdf内容不能为空");
        }
        //判断文件类型，我们只支持图片上传，比如说：png，jpg，gif
        String contentType = file.getContentType();
        log.info("contentType == >" + contentType);
        if (TextUtils.isEmpty(contentType)) {
            return ResponseResult.FAILED("pdf格式错误");
        }
        //获取相关数据，比如说文件类型，文件名称
        String originalFilename = file.getOriginalFilename();
        log.info("originalFilename == >" + originalFilename);
        String type = getType(contentType, originalFilename);
        if (type == null) {
            return ResponseResult.FAILED("不支持此文件类型");
        }
        long size = file.getSize();
        log.info("maxSize ==>" + maxSize + "size === >" + size);
        if (size > maxSize) {
            return ResponseResult.FAILED("pdf最大支持" + (maxSize / 1024 / 1024) + "Mb");
        }
        String s = UploadPdfToCosUtils.uploadPdf(file);
        log.info(s + "得到的路径是");
        //String url = Constants.COS.URL + s;
        Map<String, String> result = new HashMap<>();

        result.put("id", s);
        //第二个是名称--->alt=“图片描述”，如果不写，前端使用名称作为这个描述
        result.put("name", originalFilename);
        Pdf pdf = new Pdf();
        pdf.setId(String.valueOf(idWorker.nextId()));
        pdf.setName(originalFilename);
        pdf.setUrl(Constants.COS.URL + s);
        pdf.setCreateTime(new Date());
        pdf.setUpdateTime(new Date());
        pdf.setState("1");
        SobUser sobUser = userService.checkSobUser();
        pdf.setUserId(sobUser.getId());
        pdfDao.save(pdf);
        return ResponseResult.SUCCESS("文件上传成功").setData(result);
    }

    @Override
    public void viewPdf(HttpServletResponse response, String pdfId) throws IOException {

    }

    @Override
    public ResponseResult listPdfs(int page, int size) {
        page = checkPage(page);
        size = checkSize(size);
        //没有就往下走
        Sort sort = new Sort(Sort.Direction.DESC, "createTime");
        Pageable pageable = PageRequest.of(page - 1, size, sort);
        Page<Pdf> all =pdfDao.findAll(pageable);
        return ResponseResult.SUCCESS("获取pdf列表成功").setData(all);
    }

    @Override
    public ResponseResult deleteById(String pdfId) {
        return null;
    }

    @Override
    public void createQrCode(String code, HttpServletResponse response, HttpServletRequest request) {

    }


    public static String getType(String contentType, String name) {
        String type = null;
        if (Constants.ImageType.TYPE_PNG_WITH_PREFIX.equals(contentType)
                && name.endsWith(Constants.ImageType.TYPE_PNG)) {
            type = Constants.ImageType.TYPE_PNG;
        } else if (Constants.ImageType.TYPE_GIF_WITH_PREFIX.equals(contentType)
                && name.endsWith(Constants.ImageType.TYPE_GIF)) {
            type = Constants.ImageType.TYPE_GIF;
        } else if (Constants.ImageType.TYPE_JPG_WITH_PREFIX.equals(contentType)
                && name.endsWith(Constants.ImageType.TYPE_JPG)) {
            type = Constants.ImageType.TYPE_JPG;
            //添加一个新的pdf文件
        }else if (Constants.PdfType.TYPE_PDF_WITH_PREFIX.equals(contentType)
                && name.endsWith(Constants.PdfType.TYPE_PDF)) {
            type = Constants.PdfType.TYPE_PDF;
        }
        return type;
    }

}
